const financialService = require('../../../services/financial');
const { TYPE_OPTIONS: BASE_TYPE_OPTIONS, BANK_OPTIONS: BASE_BANK_OPTIONS } = require('../../../utils/dict'); // 新增：导入基础配置

// 扩展基础配置（添加"全部"选项）
const TYPE_OPTIONS = [{ id: '', name: '全部' }, ...BASE_TYPE_OPTIONS];
const BANK_OPTIONS = [{ id: '', name: '全部' }, ...BASE_BANK_OPTIONS];

Page({
  data: {
    loading: false,
    searchForm: {
      keyword: '',        // 产品名称关键字
      type: '',          // 产品类型
      bankId: '',        // 发行银行
      dateRange: []      // 日期范围
    },
    operations: [],      // 操作记录列表
    // 操作类型选项
    operationTypes: [
      { name: '全部', id: '' },
      { name: '买入', id: 'buy' },
      { name: '卖出', id: 'sell' }
    ],
    // 产品类型选项
    typeOptions: TYPE_OPTIONS, // 使用扩展后的配置
    bankOptions: BANK_OPTIONS  // 使用扩展后的配置
  },
    pagination: {
      current: 1,
      pageSize: 20,
      total: 0
    },
  onLoad() {
    this.loadData();
  },

  // 加载数据
  async loadData(isRefresh = false) {
    if (isRefresh) {
      this.setData({
        'pagination.current': 1
      });
    }

    try {
      this.setData({ loading: true });
      const { searchForm, pagination } = this.data;
      
      const operations = await financialService.getOperationList({
        keyword: searchForm.keyword,
        type: searchForm.type,
        bankId: searchForm.bankId,
        startDate: searchForm.dateRange[0] || '',
        endDate: searchForm.dateRange[1] || '',
        page: pagination.current,
        pageSize: pagination.pageSize
      });

      this.setData({
        operations: isRefresh ? operations.list : [...this.data.operations, ...operations.list],
        'pagination.total': operations.total,
        loading: false
      });
    } catch (error) {
      console.error('加载数据失败:', error);
      this.setData({ loading: false });
      wx.showToast({
        title: '加载失败',
        icon: 'error'
      });
    }
  },

  // 处理搜索表单变化
  handleSearchInput(e) {
    const { field } = e.currentTarget.dataset;
    const { value } = e.detail;
    
    this.setData({
      [`searchForm.${field}`]: value
    });
  },

  // 处理类型选择
  handleTypeChange(e) {
    const index = e.detail.value;
    this.setData({
      'searchForm.type': this.data.typeOptions[index].id
    });
    this.loadData(true);
  },

  // 处理银行选择
  handleBankChange(e) {
    const index = e.detail.value;
    this.setData({
      'searchForm.bankId': this.data.bankOptions[index].id
    });
    this.loadData(true);
  },

  // 处理操作类型选择
  handleOperationTypeChange(e) {
    const index = e.detail.value;
    this.setData({
      'searchForm.operationType': this.data.operationTypes[index].id
    });
    this.loadData(true);
  },

  // 处理日期范围选择
  handleDateRangeChange(e) {
    const index = e.currentTarget.dataset.index;
    const { value } = e.detail;
    const dateRange = [...this.data.searchForm.dateRange];
    dateRange[index] = value;
    
    this.setData({
      'searchForm.dateRange': dateRange
    });
    this.loadData(true);
  },

  // 搜索按钮点击
  handleSearch() {
    this.loadData(true);
  },

  // 重置搜索条件
  handleReset() {
    this.setData({
      searchForm: {
        keyword: '',
        type: '',
        bankId: '',
        operationType: '',
        dateRange: []
      }
    });
    this.loadData(true);
  },

  // 下拉刷新
  async onPullDownRefresh() {
    await this.loadData(true);
    wx.stopPullDownRefresh();
  },

  // 触底加载更多
  onReachBottom() {
    const { pagination } = this.data;
    if (this.data.operations.length >= pagination.total) {
      return;
    }
    
    this.setData({
      'pagination.current': pagination.current + 1
    });
    this.loadData();
  }
});